import { Card } from "./card";
import { Config } from "./config";
import { Tooler } from "./tooler";

export class Col {
  id;
  cards = [];
  indexCol;

  constructor(indexCol) {
    this.id = Tooler.generateRandomId()
    this.indexCol = indexCol;
  }

  play() {
    this.cards.forEach(card => card.play());
  }

  updateOrder() {
    this.cards.forEach(card => card.updateOrder());
  }

  initPosition(total, idx) {
    const yStart = -this.cards.length * Config.CARD_HEIGHT + Config.GAME_HEIGHT;
    this.cards.forEach((card, i) => {
      card.y = yStart + i * Config.CARD_HEIGHT;
      card.updateStyle()
      card.moveDistance = total * Config.CARD_HEIGHT;
      card.moveTimer = Config.SCROLL_TIMER + Config.GAP_TIMER * idx;
    })
  }

  randomCards(rows) {
    for (let i = 0; i < rows; i++) {
      const card = new Card();
      card.indexCol = this.indexCol;
      card.type = Tooler.getRandomInt(0, 10);
      card.y = i * Config.CARD_HEIGHT;
      card.updateStyle()
      this.cards.push(card);
    }
  }

  fixPosition(connList) {
    this.cards.forEach((card) => {
      let distance = 0;
      connList.forEach(i => {
        if (card.indexCol == i.indexCol && card.y < i.y) {
          distance += Config.CARD_HEIGHT
        }
      })
      card.moveDistance = distance
      card.moveTimer = Config.DROP_DOWN_TIMER
    })
  }

  addAimCards(types) {
    const ary = [];
    for (let i = 0; i < types.length; i++) {
      const card = new Card();
      card.indexCol = this.indexCol;
      card.type = types[i];
      ary.push(card);
    }
    this.cards = ary.concat(this.cards);
  }

  removeOutside() {
    this.cards = this.cards.filter(card => !card.isOutside);
  }

  removeBottom(){
    this.cards = this.cards.filter(card => !card.isBottom);
  }

  removeConn(connList) {
    this.cards = this.cards.filter(card => !connList.includes(card));
  }
}